package com.jie.other.ui.list_choose_dialog

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import androidx.compose.runtime.toMutableStateList
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.fragment.app.DialogFragment
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.SizeUtils
import com.jie.other.R

/**
 * 使用compose封装一个通用的列表选择Dialog
 */
class ListChooseDialog: DialogFragment() {

    /**
     * 列表项数据集合
     */
    private val mItemList = mutableListOf<ListItemDTO>().toMutableStateList()

    /**
     * 列表项点击事件
     */
    var mItemClickListener: ((Int, ListItemDTO) -> Unit)? = null

    /**
     * 获取屏幕宽度
     */
    private val mScreenWidth by lazy {
        ScreenUtils.getScreenWidth()
    }

    /**
     * 设置列表数据
     */
    fun setData(list: List<ListItemDTO>){
        mItemList.clear()
        mItemList.addAll(list)
    }

    /**
     * 设置列表数据
     */
    fun setData(vararg itemNames: String){
        mItemList.clear()
        itemNames.forEachIndexed { index, itemName ->
            mItemList.add(
                index,
                ListItemDTO(index.toString(), itemName)
            )
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return ComposeView(requireContext()).apply {
            setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
            initWindow()
            setContent {
                ListChooseDialogView(
                    mList = mItemList,
                    // 设置滚动列表最大高度
                    maxHeight = SizeUtils.px2dp(ScreenUtils.getScreenHeight() * 0.7f),
                    itemClickListener = { pos, item ->
                        mItemClickListener?.invoke(pos, item)
                        dismiss()
                    },
                    cancelClickListener = {
                        dismiss()
                    }
                )
            }
        }
    }

    private fun initWindow() {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)
        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        dialog?.window?.setWindowAnimations(R.style.other_dialog_in_out_bottom)
        dialog?.window?.attributes = dialog?.window?.attributes?.apply {
            gravity = Gravity.BOTTOM
            width = mScreenWidth
        }
    }

    override fun onStart() {
        super.onStart()
        dialog?.window?.setLayout(mScreenWidth,
            dialog?.window?.attributes?.height
                ?:ViewGroup.LayoutParams.WRAP_CONTENT)
        dialog?.window?.attributes = dialog?.window?.attributes?.apply {
            gravity = Gravity.BOTTOM
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mItemClickListener = null
    }

}